package remote.service;

import client.common.dto.SubmitOrderDTO;
import client.common.result.OrderDetailsVOResult;
import client.common.result.OrderListResult;
import client.common.result.Result;
import client.common.vo.OrderDetailsVO;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import remote.common.constant.UrlConstant;
import remote.common.storage.TokenStorage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出入库订单服务接口
 */
public interface OrderService {
    /**
     * 提交出入库订单
     */
    default Result submitOrder(SubmitOrderDTO requestParam) {
        String resultStr = HttpRequest.post(UrlConstant.BASE_URL + "/order/submitOrder")
                .header("Authorization", TokenStorage.getToken())
                .body(JSON.toJSONString(requestParam))
                .execute().body();
        return JSON.parseObject(resultStr, Result.class);
    }
    
    /**
     * 获取订单列表
     *
     * @param status 订单状态
     * @return 订单列表
     */
    default OrderListResult getOrderList(String status, Integer currentPage, Integer pageSize) {
        String token = TokenStorage.getToken();
        String resultStr = HttpRequest.get(
                        UrlConstant.BASE_URL + "/order/getOrderList?status="
                                + status
                                + "&currentPage=" + currentPage
                                + "&pageSize=" + pageSize
                )
                .header("Authorization", token)
                .execute().body();
        return JSON.parseObject(resultStr, OrderListResult.class);
    }
    
    /**
     * 获取订单详情
     *
     * @param orderId 订单ID
     * @return 订单详情
     */
    default OrderDetailsVOResult getOrderDetails(Long orderId) {
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/order/getOrderDetail?orderId=" + orderId)
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, OrderDetailsVOResult.class);
    }
    
    /**
     * 提交审核订单
     */
    default Result auditOrder(Long orderId, Integer status) {
        String resultStr = HttpRequest.put(UrlConstant.BASE_URL + "/order/auditOrder?orderId=" + orderId + "&status=" + status)
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, Result.class);
    }
    
    /**
     * 获取指定年月下的审核通过的订单列表
     *
     * @param year  年份
     * @param month 月份
     * @return 订单列表
     */
    default Result<List<OrderDetailsVO>> getOrderListByParam(String year, String month) {
        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("year", year);
        requestParam.put("month", month);
        System.out.println("year = " + year + ", month = " + month);
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/order/getOrderByMonth")
                .form(requestParam)
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println("resultStr = " + resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<List<OrderDetailsVO>>>() {
        });
    }
}
